import minijava.lexer.Lexer;
import minijava.lexer.Lexer.State;
import minijava.node.*;


public class MyLexer extends Lexer
{ private int count;

	
  private TComentario Comentario;
  private StringBuffer text;
  // We define a constructor
  public MyLexer(java.io.PushbackReader in)
  { 
	super(in);	
  }
  // We define a filter that recognizes nested Comentarios.
  protected void filter()
  { // if we are in the Comentario state
	if(token.getText()!=""){
		System.out.println(token.getClass() + " = " + token.getText());
		
	}
	if(state.equals(State.COMENTARIO))
    { // if we are just entering this state

      if(Comentario == null)
      { // The token is supposed to be a Comentario.
        // We keep a reference to it and set the count to one

        Comentario = (TComentario) token;
        text = new StringBuffer(Comentario.getText());
        count = 1;
        token = null; // continue to scan the input.
      }
      else
      { // we were already in the Comentario state
	
        text.append(token.getText()); // accumulate the text.
	if(token instanceof TComentario)
          count++;
        else if(token instanceof TComentarioFim)
          count--;
        if(count != 0 || count < 0){
	  if(token.getText().isEmpty()){
	  	System.out.println("Comentários desbalenceados!");
		System.exit(1);
   	  }
          token = null; // continue to scan the input.

        }else
        { 
          token = Comentario; //return a Comentario with the full text.
          state = State.NORMAL; //go back to normal.
          Comentario = null; // We release this reference.
        }
      }
    }
  }
}
